//
//  FeedbackViewController.m
//  KiklinkApp
//
//  Created by LJC on 14/10/22.
//  Copyright (c) 2014年 Kiklink. All rights reserved.
//

#import "FeedbackViewController.h"

#import "UIColor+hex.h"
#import "UIView+loadWithNib.h"

#import "FeedbackCell.h"

@interface FeedbackViewController ()
{
    FeedbackCell *feedbackCell;
    NSUserDefaults *ud;
}
@end

@implementation FeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"反馈意见";
    self.view.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];

    
    //leftNavItem
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [cancelButton setTitle:@"返回" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor colorWithHexString:AppColor_Text] forState:UIControlStateNormal];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [cancelButton setFrame:CGRectMake(0, 0, NavBarHeight, NavBarHeight/2)];
    [cancelButton addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftNavBarItem = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];
    self.navigationItem.leftBarButtonItem = leftNavBarItem;
    
    
    //rightNavItem
    UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [sendButton setTitle:@"发送" forState:UIControlStateNormal];
    [sendButton setTitleColor:[UIColor colorWithHexString:AppColor_Text] forState:UIControlStateNormal];
    sendButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [sendButton setFrame:CGRectMake(0, 0, NavBarHeight, NavBarHeight/2)];
    [sendButton addTarget:self action:@selector(send) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightNavBarItem = [[UIBarButtonItem alloc] initWithCustomView:sendButton];
    self.navigationItem.rightBarButtonItem = rightNavBarItem;
    
    
    //backTap
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    tap.numberOfTapsRequired = 1;
    [self.view addGestureRecognizer:tap];
    
    
    //feedbackTable
    self.feedbackTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.feedbackTable.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];
    self.feedbackTable.delegate = self;
    self.feedbackTable.dataSource = self;
    self.feedbackTable.scrollEnabled = NO;
    
    
    //feedbackCell
    feedbackCell = [FeedbackCell loadWithNib:@"FeedbackCell" AndPostion:CGPointZero];
    feedbackCell.senderText.delegate = self;
    feedbackCell.subjectText.delegate = self;
    feedbackCell.suggestionText.delegate = self;
    
    ud = [NSUserDefaults standardUserDefaults];
    feedbackCell.senderText.text = [ud objectForKey:@"sender"];
    feedbackCell.subjectText.text = [ud objectForKey:@"subject"];
    if (![(NSString *)[ud objectForKey:@"suggestion"] isEqualToString:@""]) {
        feedbackCell.suggestionPlaceholder.hidden = YES;
    }
    feedbackCell.suggestionText.text = [ud objectForKey:@"suggestion"];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - navItemAction
- (void)cancel
{
    UIActionSheet *cancelSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"删除草稿" otherButtonTitles:@"存储草稿", nil];
    [cancelSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
            [ud setObject:@"" forKey:@"sender"];
            [ud setObject:@"" forKey:@"subject"];
            [ud setObject:@"" forKey:@"suggestion"];
            [self.navigationController popViewControllerAnimated:YES];
            break;
        case 1:
            [ud setObject:feedbackCell.senderText.text forKey:@"sender"];
            [ud setObject:feedbackCell.subjectText.text forKey:@"subject"];
            [ud setObject:feedbackCell.suggestionText.text forKey:@"suggestion"];
            [self.navigationController popViewControllerAnimated:YES];
            break;
        case 2:
            break;
        default:
            break;
    }
}
- (void)send
{
    ////api
}
#pragma mark - navItemAction End

#pragma mark - tap
- (void)tap
{
    NSLog(@"tap");
    [feedbackCell.senderText resignFirstResponder];
    [feedbackCell.subjectText resignFirstResponder];
    [feedbackCell.suggestionText resignFirstResponder];
    [self.feedbackTable setContentOffset:CGPointMake(0, 0)];
}
#pragma mark - tap End

#pragma mark - textField delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == feedbackCell.senderText) {
        [self.feedbackTable setContentOffset:CGPointMake(0, 0)];
    } else if (textField == feedbackCell.subjectText) {
        [self.feedbackTable setContentOffset:CGPointMake(0, 55)];
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == feedbackCell.senderText) {
        [feedbackCell.senderText resignFirstResponder];
        [feedbackCell.subjectText becomeFirstResponder];
    } else if (textField == feedbackCell.subjectText) {
        [feedbackCell.subjectText resignFirstResponder];
        [self.feedbackTable setContentOffset:CGPointMake(0, 0)];
    }
    return YES;
}
#pragma mark - textField delegate End

#pragma mark - textView delegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    [self.feedbackTable setContentOffset:CGPointMake(0, 125)];
}

- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length == 0) {
        feedbackCell.suggestionPlaceholder.hidden = NO;
    } else {
        feedbackCell.suggestionPlaceholder.hidden = YES;
    }
}
#pragma mark - textView delegate End

#pragma tableView dataSource&delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return feedbackCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 416;
}
#pragma tableView dataSource&delegate End

@end
